/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package persistencia;

import excepciones.NonexistentEntityException;
import excepciones.PreexistingEntityException;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityNotFoundException;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.transaction.UserTransaction;
import negocio.Funcion;
import negocio.Ticket;
import negocio.TransaccionVentas;

/**
 *
 * @author Diego
 */
public class TransaccionVentasJpaController implements Serializable  {
    
    private UserTransaction utx = null;
    private EntityManagerFactory emf = null;
    
    public TransaccionVentasJpaController(UserTransaction utx, EntityManagerFactory emf) {
        this.utx = utx;
        this.emf = emf;
    }

    public TransaccionVentasJpaController() {
        emf = Persistence.createEntityManagerFactory("entradasJpaPU");
    }
    
    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }
    
    public TransaccionVentas findTransaccionVentas(Integer id){
        EntityManager em = getEntityManager();
        try {
            return em.find(TransaccionVentas.class, id);
        } finally {
            em.close();
        }
    }
    public void destroy (String identificador) throws NonexistentEntityException{
        EntityManager em = null;
        
        try{
            em = getEntityManager();
            em.getTransaction().begin();
            TransaccionVentas transaccionVentas;
            try{
                transaccionVentas = em.getReference(TransaccionVentas.class, identificador);
                transaccionVentas.getId();
                }
            catch(EntityNotFoundException ex){
                 throw new NonexistentEntityException("Transaccion Inexistente");    
                }
            em.remove(transaccionVentas);
            em.getTransaction().commit();
        }
        finally{
            if (em != null) {
                em.close();
            } 
        }
    }
    
    public void edit(TransaccionVentas transaccionVentas)   throws PreexistingEntityException, Exception {
        EntityManager em = null;
        try{
            em = getEntityManager();
            em.getTransaction().begin();
            transaccionVentas = em.merge(transaccionVentas);
            em.getTransaction().commit();
        }
        catch (Exception ex) {
             String msg = ex.getLocalizedMessage();
             if(msg == null || msg.length() == 0){
                 if(findTransaccionVentas(transaccionVentas.getId()) == null){
                     throw new NonexistentEntityException("Transaccion Inexistente");
                 }
             }
        }
        finally {
            if (em != null) {
                em.close();
            }
        }
    }
    public void create(TransaccionVentas transaccionVentas)  throws PreexistingEntityException, Exception {
        EntityManager em = null;
        try{
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(transaccionVentas);
            em.getTransaction().commit();
        }
        catch(Exception ex){
            if(findTransaccionVentas(transaccionVentas.getId()) != null){
                throw new PreexistingEntityException("Transaccion Existente",ex);
            }
            throw ex;
        }
        finally{
            if(em != null){
                em.close();
            }
        }
        
    }
    
    
    public List<TransaccionVentas> findtransaccionVentasEntities() {
        return findtransaccionVentasEntities(true, -1, -1);
    }
    
    private  List  findtransaccionVentasEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from TransaccionVentas as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }
    public List<Ticket> findTicketsVendidos(Funcion funcion){
        EntityManager em = getEntityManager();
        try{
            Query q = em.createQuery("select object(o) from Ticket as o where o.unaFuncion = ?1");            
            return q.getResultList();
        } finally {
            em.close();
        }
    }
}
